﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Const;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class CSettingsViewModel : CCommandViewModel
    {
        string m_CurrentDetailResourceName = "";
        string m_sCurrentPathName = "";
        CViewModelBase _CurrentOptionsDetailViewModel = null;
        WinKStartSettings _UserSettingsObject = null;

        public CSingleCommandViewModel Apply { get { return m_oCommands[0]; } }
        public CSingleCommandViewModel Discard { get { return m_oCommands[1]; } }
        public CSingleCommandViewModel Cmd_SelectedSettingChanged { get { return m_oCommands[2]; } } 
        public string CategoryCaption { get; protected set; }

        public CViewModelBase CurrentDetailViewModel { get; set; }

        public string CurrentDetailResourceName {
            get { return m_CurrentDetailResourceName; }
            set { 
                m_CurrentDetailResourceName = value;
                OnPropertyChanged("CurrentDetailResourceName");
            }
        }

        public string CurrentPathName
        {
            get { return m_sCurrentPathName; }
            set
            {
                m_sCurrentPathName = value;
                OnPropertyChanged("CurrentPathName");
            }
        }

        public CViewModelBase CurrentOptionsDetailViewModel
        {
            get { return _CurrentOptionsDetailViewModel; }
            set {
                _CurrentOptionsDetailViewModel = value;
                OnPropertyChanged("CurrentOptionsDetailViewModel");
            }
        }

        public List<CSingleSettingOptionViewModel> SettingOptions { get; private set; } 

        public CSettingsViewModel()
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("Settings");
                CategoryCaption = LanguageStrings.GetText("Category");

                //System.Windows.Controls.TreeViewItem tmpItem = new System.Windows.Controls.TreeViewItem();
                //tmpItem.Header = "WinKStart";
                //tmpItem.Items.Add(new CSingleSettingOptionViewModel(LanguageStrings.GetText("Behavior"),
                //                                                    LanguageStrings.GetText("WinKStart") + " - " + LanguageStrings.GetText("Behavior"),
                //                                                    new CWinKStartBehaviorViewModel(_UserSettingsObject.Behavior)));

                //Load settings from file
                string sUserSettingsPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),
                                                                  CConfigElements.APPLICATION_CONFIG_SUBPATH);
                sUserSettingsPath = System.IO.Path.Combine(sUserSettingsPath, CConfigElements.USERCONFIG_FILENAME);
                _UserSettingsObject = cUserSettingsReader.LoadSettingsObject(sUserSettingsPath);

                string sConfigPath = System.IO.Path.Combine(cUserSettingsReader.ConfigPath, @"content.xml");
                CContentConfig tmpContentConfig = cMenuConfigReader.LoadContentConfig(sConfigPath);

                CWinKStartBehaviorViewModel tmpBehaviorVM = new CWinKStartBehaviorViewModel(_UserSettingsObject.Behavior);
                CContentActionsViewModel tmpContentActionsVM = new CContentActionsViewModel(tmpContentConfig, tmpContentConfig.Actions);
                CModulesDataSourceViewModel tmpModulesVM = new CModulesDataSourceViewModel(tmpContentConfig.DataSources);

                SettingOptions = new List<CSingleSettingOptionViewModel>();
                SettingOptions.Add(new CSingleSettingOptionViewModel(LanguageStrings.GetText("WinKStart"),
                                                                     LanguageStrings.GetText("WinKStart") + " - " + LanguageStrings.GetText("Behavior"),
                                                                     tmpBehaviorVM,
                                                                     System.Windows.FontWeights.Bold, new List<CSingleSettingOptionViewModel>
                                                                    { new CSingleSettingOptionViewModel(LanguageStrings.GetText("Behavior"), 
                                                                                                        LanguageStrings.GetText("WinKStart") + " - " + LanguageStrings.GetText("Behavior"),
                                                                                                        tmpBehaviorVM),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("StartButton"), 
                                                                                                        LanguageStrings.GetText("WinKStart") + " - " + LanguageStrings.GetText("StartButton"),
                                                                                                        new CWinKStartStartButtonViewModel(_UserSettingsObject.StartButton)),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Context Menu"), 
                                                                                                        LanguageStrings.GetText("WinKStart") + " - " + LanguageStrings.GetText("Context Menu"),
                                                                                                        new CWinKStartContextMenuViewModel(_UserSettingsObject.ContextMenu)) 
                                                                 }));
                SettingOptions.Add(new CSingleSettingOptionViewModel(LanguageStrings.GetText("Content"),
                                                                     LanguageStrings.GetText("Content") + " - " + LanguageStrings.GetText("Actions"),
                                                                     tmpContentActionsVM,
                                                                     System.Windows.FontWeights.Bold,
                                                                     new List<CSingleSettingOptionViewModel>
                                                                    { new CSingleSettingOptionViewModel(LanguageStrings.GetText("Actions"), 
                                                                                                        LanguageStrings.GetText("Content") + " - " + LanguageStrings.GetText("Actions"),
                                                                                                        tmpContentActionsVM),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Context Menu"), 
                                                                                                        LanguageStrings.GetText("Content") + " - " + LanguageStrings.GetText("Context Menu"),
                                                                                                        new CContentContextMenuViewModel(tmpContentConfig.ContextMenuTargets, tmpContentConfig)),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Regions"), 
                                                                                                        LanguageStrings.GetText("Content") + " - " + LanguageStrings.GetText("Regions"),
                                                                                                        new CContentRegionsViewModel(tmpContentConfig.Regions)) 
            }));
                SettingOptions.Add(new CSingleSettingOptionViewModel(LanguageStrings.GetText("Modules"),
                                                                     LanguageStrings.GetText("Modules") + " - " + LanguageStrings.GetText("DataSource"),
                                                                     tmpModulesVM,
                                                                     System.Windows.FontWeights.Bold,
                                                                     new List<CSingleSettingOptionViewModel>
                                                                    { new CSingleSettingOptionViewModel(LanguageStrings.GetText("DataSource"), 
                                                                                                        LanguageStrings.GetText("Modules") + " - " + LanguageStrings.GetText("DataSource"),
                                                                                                        tmpModulesVM),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Engines"), 
                                                                                                        LanguageStrings.GetText("Modules") + " - " + LanguageStrings.GetText("Engines"),
                                                                                                        new CModulesEnginesViewModul(tmpContentConfig.SearchEngines)),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Functions"), 
                                                                                                        LanguageStrings.GetText("Modules") + " - " + LanguageStrings.GetText("Functions"),
                                                                                                        new CModulesFunctionViewModel(tmpContentConfig.Functions)),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Styles"), 
                                                                                                        LanguageStrings.GetText("Modules") + " - " + LanguageStrings.GetText("Styles"),
                                                                                                        new CModulesStylesViewModel()),
                                                                      new CSingleSettingOptionViewModel(LanguageStrings.GetText("Languages"), 
                                                                                                        LanguageStrings.GetText("Modules") + " - " + LanguageStrings.GetText("Languages"),
                                                                                                        new CModulesLanguageViewModel()) 
            }));
                SettingOptions.Add(new CSingleSettingOptionViewModel(LanguageStrings.GetText("About"), LanguageStrings.GetText("About"), new CAboutViewModel(), System.Windows.FontWeights.Bold));

                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.ExecuteApplyData())));
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Discard"), new CDelegatingCommand(Delegate => this.ExecuteDiscardData())));
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("SelectedSettingChanged"), new CDelegatingCommand(Delegate => this.ExecuteDiscardData())));
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to initialize Settings!", Ex, EMessageTypes.Error);
            }
        }

        //TODO: implements a View-Model to describe the option details
        public ReadOnlyCollection<CSingleSettingDetailViewModel> Views 
        {
            get 
            {
                return null;
            } 
        }

        /// <summary>
        /// Set current from user selected TreeView-Item to SettingsViewModel
        /// </summary>
        /// <param name="Item">Selected Item</param>
        public void SetSelectedOption(CSingleSettingOptionViewModel Item)
        {
            try
            {
                if (Item != null)
                {
                    CurrentPathName = Item.ViewPathName;                    
                    CurrentOptionsDetailViewModel = Item.ViewModel;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to set Selected Option!", Ex, EMessageTypes.Error);
            }
        }

        void ExecuteApplyData()
        {
            try
            {
                Settings ParentWindow = null;
                if ((Settings)((CDelegatingCommand)Apply.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings)((CDelegatingCommand)Apply.Command).CommandParameter;
                }
                //TODO: Apply ContextMenu Values from ViewModel
                _UserSettingsObject.ContextMenu = ((CWinKStartContextMenuViewModel)SettingOptions[0].Items[2].ViewModel).ApplyChangedData();
                {
                    if (_UserSettingsObject != null)
                    {
                        string sUserSettingsPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),
                                                  CConfigElements.APPLICATION_CONFIG_SUBPATH);
                        sUserSettingsPath = System.IO.Path.Combine(sUserSettingsPath, CConfigElements.USERCONFIG_FILENAME);
                        //Serialize settings object
                        cUserSettingsReader.SaveSettings(sUserSettingsPath, _UserSettingsObject);

                        if (ParentWindow != null)
                            ParentWindow.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to save changes!", Ex, EMessageTypes.Error);
            }
        }

        void ExecuteDiscardData()
        {
            //TODO: implements code
            try
            {
                Settings ParentWindow = null;
                if ((Settings)((CDelegatingCommand)Discard.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings)((CDelegatingCommand)Discard.Command).CommandParameter;

                    if (ParentWindow != null)
                        ParentWindow.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to discard changes!", Ex, EMessageTypes.Error);
            }
            
        }
    }
}
